#!/bin/python

# TODO: 2 tmpfs: /var/log  ;  /var/tmp/

hddDev = "/dev/sda"

tfile = "/tmp/hsp.txt"

advancedPM = "AdvancedPM="

import os, sys

### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### 
#
# noatime
#
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### 
os.system("mount > " + tfile)
mountBuf = open(tfile).read()
corrStrs = []
for bstr in mountBuf.split("\n"):
	if bstr.find(hddDev) > -1 and bstr.find("noatime") == -1:
		on = "on"
		pon = bstr.find(on)
		if pon == -1:
			raise "can't find " + on + " in " + bstr
		stype = "type"
		pstype = bstr.find(stype)
		if pstype == -1:
			raise "can't find " + stype + " in " + bstr
		bstr = bstr[pon + len(on) + 1:pstype-1]
		corrStrs.append(bstr)
if len(corrStrs) == 0:
	print "\t\tnoatime\t\t\t\talready set"
else:
	print corrStrs
	notImplemented()
	for mntp in corrStrs:
		os.system("moune -oremount,noatime " + mntp)
	


### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### 
#
# AdvancedPM
#
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### 
os.system("hdparm -B -i " + hddDev + " > " + tfile)
advInfoBuf = open(tfile).read()
p = advInfoBuf.find(advancedPM)
if p == -1:
	print "unrecognized format of AdvancedPM info"
	exit()
pa = p + len(advancedPM)
ans = advInfoBuf[pa:pa+3]
if ans != "yes":
	ans = ans[:2]
	if ans != "no":
		print "unrecognized format of AdvancedPM info: " + ans
		exit()
ansb = ans == "yes"
if ansb:
	print("\t\t" + advancedPM[:-1] + "\t\t\talready set")
else:
	print("setup " + advancedPM[:-1] + "? (y/n)")
	notImplemented()
	mode = 1 # the most agressive
	timeSec = 2 * 60 + 3 # 2m 30s
	time = timeSec / 5
	os.system("hdparm -B " + mode + " -S " + time + " " + hddDev)
	print advancedPM[:-1], "setup OK"


### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### 
#
# writeback time
#
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### 
wbBuf = open("/proc/sys/vm/dirty_writeback_centisecs").read()
wbtime = int(wbBuf)
wbtimeSec = float(wbtime) / 100.0
print "writeback time is:", wbtimeSec, \
	"s. would you like to change it? (print new walue in seconds or 'n'):"
newValSec = 15.0
newVal = int(newValSec * 100)
newValBuf = str(newVal) + "\n"
if newValBuf == wbBuf:
	print "\t\twriteback time \t\t\tis already", newValSec
else:
	notImplemented()
	f = opn("/proc/sys/vm/dirty_writeback_centisecs", "w")
	f.write(newValBuf)
	f.close()
	print "writeback time was set to", newValSec, "OK"


### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### 
#
# laptop mode
#
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### 
lmBuf = open("/proc/sys/vm/laptop_mode").read()
lmInt = int(lmBuf)
if lmInt == 5:
	print "\t\tlaptop mode \t\t\tis already activ"
else:
	print "laptop mode is not activ, activate? (y/n)"
	notImplemented()
	f = opn("/proc/sys/vm/laptop_mode", "w")
	f.write(str(5) + "\n")
	f.close()
	print "laptop mode activated OK"
	

### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### 
#
# disable cdrom
#
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### 

